package com.erp.order.purchasing.dao.impl;

import com.erp.hr.base.dao.impl.BaseDaoImpl;
import com.erp.hr.base.util.CommonUtils;
import com.erp.order.purchasing.dao.PurchaseOrderDao;
import com.erp.order.purchasing.model.PurchaseOrder;
import com.erp.order.purchasing.model.PurchasingRequisitionDetail;
import org.apache.commons.lang3.StringUtils;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单 持久实现层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Repository
@Transactional
public class PurchaseOrderDaoImpl extends BaseDaoImpl<PurchaseOrder> implements PurchaseOrderDao {

    /**
     * dao支持
     */
    @Autowired
    private DaoSupport daoSupport;


    /**
     * 根据id查询对象
     * @param id 对象id
     * @return 查询结果
     */
    @Override
    public PurchaseOrder getDataObject(String id) {
        String sql =
                "SELECT " +
                    "po.* " +
                "FROM " +
                    "purchase_order po " +
                "WHERE " +
                    "po.id = :id " +
                "AND " +
                    "po.is_del = :isDel";
        Map<String, Object> args = new HashMap<>();
        args.put("id", id);
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("po", PurchaseOrder.class);
        List<PurchaseOrder> list = daoSupport.selectDataSql(sql, entity, args);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public List<PurchaseOrder> getDataObjects(PurchaseOrder purchaseOrder) {
        return null;
    }

    /**
     * 获取列表分页数据
     * @param dataAuthSQL 数据验证sql
     * @param pages 分页对象
     * @param purchaseOrder 查询条件对象
     * @return 列表分页数据
     */
    @Override
    public List<PurchaseOrder> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, PurchaseOrder purchaseOrder) {
        String sql =
                "SELECT " +
                    "po.* " +
                "FROM " +
                    "purchase_order po " +
                "WHERE " +
                    "1 = 1 ";
        Map<String, Object> args = new HashMap<>();
        if (StringUtils.isNotBlank(purchaseOrder.getCode())) {
            purchaseOrder.setCode("%" + purchaseOrder.getCode() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(purchaseOrder, "code", "and po.", args);
        }
        if (StringUtils.isNotBlank(purchaseOrder.getCompanyName())) {
            purchaseOrder.setCompanyName("%" + purchaseOrder.getCompanyName() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(purchaseOrder, "companyName", "and po.", args);
        }
        if (StringUtils.isNotBlank(purchaseOrder.getSupplierName())) {
            purchaseOrder.setSupplierName("%" + purchaseOrder.getSupplierName() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(purchaseOrder, "supplierName", "and po.", args);
        }
        sql +=
                "AND " +
                    "po.is_del = '0' " +
                "ORDER BY " +
                    "po.update_time DESC";
        daoSupport.validateDataObject(purchaseOrder);
        return daoSupport.getDataSqlByPage(
                sql,
                new HashMap<String, Class<?>>(){{
                    put("po", PurchaseOrder.class);
                }},
                args,
                pages);
    }

    /**
     * 获取时间范围内的创建数量
     * @return 时间范围内的申请数量
     */
    @Override
    public Integer getCreateCount() {
        String sql =
                "SELECT " +
                    "po.* " +
                "FROM " +
                    "purchase_order po " +
                "WHERE " +
                    "po.is_del = :isDel " +
                "AND " +
                    "DATE_FORMAT(po.create_time, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')";
        Map<String, Object> args = new HashMap<>();
        args.put("isDel", CommonUtils.IS_DEL_NO);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("po", PurchaseOrder.class);
        List<PurchaseOrder> result = daoSupport.selectDataSql(sql, entity, args);
        return result == null ? 0 : result.size();
    }

    /**
     * 获取采购订单详情
     * @param id 采购订单id
     * @return 采购订单详情集合
     */
    @Override
    public List<PurchasingRequisitionDetail> getSubInfo(Long id) {
        return daoSupport.selectDataSql(
                "SELECT " +
                        "prd.* " +
                    "FROM " +
                        "purchasing_requisition_detail prd " +
                    "WHERE " +
                        "prd.purchase_order_id = :id " +
                    "AND " +
                        "prd.is_del = :isDel",
                new HashMap<String, Class<?>>(){{
                    put("prd", PurchasingRequisitionDetail.class);
                }},
                new HashMap<String, Object>(){{
                    put("id", id);
                    put("isDel", CommonUtils.IS_DEL_NO);
                }});
    }

    @Override
    public List<PurchaseOrder> getDatasBySupplyid(String username,PurchaseOrder purchaseOrder,Pages pages) {
        String sql="SELECT po.* FROM purchase_order po " +
                   "WHERE po.supplier_id=(SELECT m.vendor_id FROM md_vendor m WHERE m.credit_code=:username)  " +
                   "AND po.status!=0 ";
        Map<String, Object> args = new HashMap<>();
        if (org.apache.commons.lang.StringUtils.isNotBlank(purchaseOrder.getCode())) {
            purchaseOrder.setCode("%" + purchaseOrder.getCode() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(purchaseOrder, "code", "and po.", args);
        }
        if (org.apache.commons.lang.StringUtils.isNotBlank(purchaseOrder.getSupplierName())) {
            purchaseOrder.setSupplierName("%" + purchaseOrder.getSupplierName() + "%");
            sql += DaoCRUDUtil.getSQLLikeCondition(purchaseOrder, "supplierName", "and po.", args);
        }
        args.put("username", username);
        sql = sql + " order by po.create_time desc";
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("po", PurchaseOrder.class);
        List<PurchaseOrder> results = daoSupport.getDataSqlByPage(sql, entity, args,pages);
        return results ;
    }

    @Override
    public PurchaseOrder getDataObject(Long id) {
        String sql = "select c.* from purchase_order c where c.id = :id";

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("id", id);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("c", PurchaseOrder.class);

        List list = this.daoSupport.selectDataSql(sql, entity, args);

        if(list.size()>0) {
            return (PurchaseOrder)list.get(0);
        }

        return null;
    }

}